package SHIYAN;

import java.util.*;

class Student {

    String name;
    int math;
    int english;
    int computer;

    //默认构造方法
    public Student() {
    }

    //带参数构造方法
    public Student(String name, int math, int english,
                   int computer) {
        this.name = name;
        this.math = math;
        this.english = english;
        this.computer = computer;
    }

    @Override
    public String toString() {
        return "[name=" + name + ", math=" + math
                + ", english=" + english + ", computer=" + computer + "]";
    }

}

class MyComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        // TODO Auto-generated method stub
        return (int) (o1.math + o1.english) - (o2.math + o2.english);
    }
}

public class ObjectArraySort {
    //冒泡排序
    public static void bubbleSort(Object[] arr, Comparator comp) {
        for (int i = 0; i < arr.length - 1; i++) {
            //每次把最大元素交换到最后位置
            for (int j = 0; j < arr.length - 1; j++) {
                if (comp.compare(arr[i], arr[i + 1]) > 0) {
                    Object temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    public static void main(String[] args) {
        Student[] arr2 = {
                new Student("Liubei", 60, 60, 60),
                new Student("zhangfei", 90, 60, 90),
                new Student("Liubei", 60, 80, 60),
        };
        bubbleSort(arr2, new MyComparator());
    }
}